package com.example.exception;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.io.IOException;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler{

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerException(BusinessException businessException,WebRequest request){
        System.out.println("进去全局自定义异常类了");
        Map<Object, Object> body = new HashMap<>();
        body.put("timestamp", DateFormat.getDateTimeInstance(1,2).format(new Date()));
        body.put("status",businessException.getHttpStatus().value());
        body.put("message",businessException.getMessage());
        body.put("path",request.getDescription(false).substring(4));
        return new ResponseEntity(body, HttpStatus.OK);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerALLException(Exception exception,WebRequest request){
        System.out.println("进去全局未知错误异常类了");
        System.out.println(exception.getMessage());
        Map<Object, Object> body = new HashMap<>();
        body.put("timestamp", DateFormat.getDateTimeInstance(1,2).format(new Date()));
        body.put("status",HttpStatus.BAD_REQUEST.value());
        body.put("message","Internal Server error");
        body.put("path",request.getDescription(false).substring(4));
        body.put("exception",exception.getMessage());
        return new ResponseEntity(body, HttpStatus.OK);
    }

    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity handlerNullException(NullPointerException exception,WebRequest request){
        System.out.println("进去空指针异常类了");
        Map<Object, Object> body = new HashMap<>();
        body.put("timestamp", DateFormat.getDateTimeInstance(1,2).format(new Date()));
        body.put("status",HttpStatus.BAD_REQUEST.value());
        body.put("message","对象未定义异常");
        body.put("path",request.getDescription(false).substring(4));
        return new ResponseEntity(body, HttpStatus.OK);
    }

    @ExceptionHandler(IOException.class)
    public ResponseEntity handlerIoException(IOException exception,WebRequest request){
        System.out.println("进去全局IO错误异常类了");
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setStatus(HttpStatus.BAD_REQUEST.value());
        errorResponse.setMessage("IO异常");
        errorResponse.setPath(request.getDescription(false).substring(4));
        return new ResponseEntity(errorResponse, HttpStatus.OK);
    }









}
